package com.example.yuejian.yuejian.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.Collection;
import com.example.yuejian.yuejian.bean.IsFollow;
import com.example.yuejian.yuejian.model.CollectionModel;
import com.example.yuejian.yuejian.view.WaitingView;


public class MatchDetailActivity extends BaseActivity {
    private ImageView btn_back;
    private TextView title;
    private Button back;
    private Dialog loadingDialog;
    private WebView webView;

    private Intent intent;
    private WaitingView waitingView;
    private ProgressBar progressBar;
    private static final int LOADFINISH=100;
    private String url="http://www.neusoft.edu.cn/";
      String match_id;
    private ImageView btn_follow;
    private ModleListener<Collection> collectlistener=new ModleListener<Collection>() {
        @Override
        public void onResponse(Collection o) {
            Collection res=o;
            int stutas=res.getRetCode();
            if (1==stutas)
            {
                btn_follow.setImageResource(R.drawable.ic_follow_white);
                Toast.makeText(MatchDetailActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            }
            else
            {
                btn_follow.setImageResource(R.drawable.ic_follow);
                Toast.makeText(MatchDetailActivity.this, res.getRetMsg(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MatchDetailActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
        }
    };
    private ModleListener<IsFollow> isfollowlistener=new ModleListener<IsFollow>() {
        @Override
        public void onResponse(IsFollow o) {
            IsFollow res=o;
            int stutas=res.getRetCode();
            if (1==stutas)
            {
                btn_follow.setImageResource(R.drawable.ic_follow);
            }
            else
            {
                btn_follow.setImageResource(R.drawable.ic_follow_white);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MatchDetailActivity.this, "cuowu", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_detail);

        initView();
         back();
        getData();
        isFollow();

        initEvents();
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                waitingView.setVisibility(View.GONE);
            }
        });
        progressBar=(ProgressBar)findViewById(R.id.progress);
        webView.loadUrl( url);
        waitingView.setVisibility(View.VISIBLE);
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if(newProgress==LOADFINISH){
                    progressBar.setVisibility(View.GONE);
                }else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
    }
    private void initView() {
        btn_follow=(ImageView)findViewById(R.id.match_detail_follow_img_btn);

        waitingView=(WaitingView) findViewById(R.id.id_wv);
        webView=(WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        waitingView.stop();
    }

    private void back()
      {
        btn_back=(ImageView)findViewById(R.id.imgBtn_match_detail_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
         }
    private void getData()
    {
        Intent intent=getIntent();
        match_id=intent.getStringExtra("match_id");
        Toast.makeText(getApplication(),match_id,Toast.LENGTH_SHORT).show();
    }
          private void initEvents()
             {
              btn_follow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            doFollow();
        }
    });
          }

    private void isFollow()
    {
        int collection_id=Integer.parseInt(match_id);
        //int user_id=Integer.parseInt(getUser_id());
        CollectionModel collectionModel=new CollectionModel();
        collectionModel.isfollow(getUser_id(),4,collection_id,isfollowlistener);

    }
    private  void doFollow()
    {
        int collection_id=Integer.parseInt(match_id);
        int user_id=Integer.parseInt(getUser_id());
        CollectionModel collectionModel=new CollectionModel();
        collectionModel.followMaster(user_id,4,collection_id,collectlistener);

    }

        }
